layui.use(['admin', 'ax', 'table' ,'element' ,'form'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var admin = layui.admin;
    var table = layui.table;
    var form = layui.form;

    var Current = {
        tableId: "table",
        data: [] ,
        checkedData: []
    };

    Current.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'code', hide: true, sort: true, title: 'code'},
            {field: 'name', sort: true, title: '验证码名称'},
            {field: 'status', sort: true, title: '状态' ,templet: function (d) {
                    return d.status ? "开启" : "关闭";
            }},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    Current.onChangeStatus = function (data) {
        var uri = data.status ? "/captcha/cancel/valid" : "/captcha/add/valid";
        var msg = data.status ? "关闭" : "开启";
        var operation = function () {
            var ajax = new $ax(system.contextPath + uri , function (res) {
                if (res.status) {
                    system.success(msg + "成功!");
                    table.reload(Current.tableId);
                } else {
                    system.error( msg + "失败!" + res.msg + "!");
                }
            }, function (res) {
                system.error(msg + "失败!" + res.msg + "!");
            });
            ajax.set({validCode: data.code});
            ajax.start();
        };
        system.confirm("是否" + msg + "?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Current.tableId,
        url: system.contextPath + '/captcha/search/all',
        page: false,
        height: "full-158",
        cellMinWidth: 100,
        cols: Current.initColumn()  ,parseData: function(res){ //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                //"count": res.data.total, //解析数据长度
                "data": res.data //解析数据列表
            };
        },
        response: {
            statusCode: 200
        }
        ,size: 'lg',
        done: function (res, curr, count) {
            count || this.elem.next('.layui-table-view').find('.layui-table-header').css('overflow', 'visible');
            layui.each($('select'), function (index, item) {
                var elem = $(item);
                elem.val(elem.data('value')).parents('div.layui-table-cell').css('overflow', 'visible');
            });
            form.render();
        }
    });

    // 工具条点击事件
    table.on('tool(' + Current.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'switch') {
            Current.onChangeStatus(data);
        }
    });
});
